package com.youcash.ZyWallet_spring.service.impl;

import com.youcash.ZyWallet_spring.dao.CustomersLoansMapper;
import com.youcash.ZyWallet_spring.entity.CustomersLoans;
import com.youcash.ZyWallet_spring.entity.CustomersLoansVO;
import com.youcash.ZyWallet_spring.service.CustomersLoansService;
import com.youcash.ZyWallet_spring.vo.ResStatus;
import com.youcash.ZyWallet_spring.vo.ResultVO;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class CustomersLoansServiceImpl implements CustomersLoansService {

    @Autowired
    private CustomersLoansMapper customersLoansMapper;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

    @Override
    public ResultVO submitApplication(CustomersLoans customersLoans) {
        customersLoans.setApplytime(sdf.format(new Date()));
        customersLoans.setUpdatetime(sdf.format(new Date()));
        int i = customersLoansMapper.insert(customersLoans);
        if (i > 0) {
            return new ResultVO(ResStatus.OK, "添加成功", null);
        } else {
            return new ResultVO(ResStatus.NO, "添加失败", null);
        }
    }

    @Override
    public ResultVO listApplication() {
        List<CustomersLoansVO> customersLoans = customersLoansMapper.selectCustomersLoans();
        if (customersLoans.size() == 0) {
            return new ResultVO(ResStatus.NO, "查询失败！", null);
        } else {
            return new ResultVO(ResStatus.OK, "查询成功！", customersLoans);
        }
    }

    @Override
    public ResultVO listApplicationById(int id) {
        List<CustomersLoansVO> customersLoans = customersLoansMapper.selectCustomersLoansById(id);
        if (customersLoans.size() != 0) {
            return new ResultVO(ResStatus.OK, "查询成功！", customersLoans);
        } else {
            return new ResultVO(ResStatus.NO, "查询失败！", null);
        }
    }

    @Override
    public ResultVO getStatusById(int id) {
        CustomersLoans customersLoans = customersLoansMapper.selectByPrimaryKey(id);
        return new ResultVO(ResStatus.OK, "更新成功", customersLoans.getStatus());
    }


    @Override
    public ResultVO updateStatus(int id, int status) {
        int i = customersLoansMapper.updateStatusById(id, status);
        if (i > 0) {
            return new ResultVO(ResStatus.OK, "更新成功", null);
        } else {
            return new ResultVO(ResStatus.NO, "更新失败", null);
        }
    }

    @Override
    public ResultVO listStatusByCustomersId(int customersId, int status) {
        List<CustomersLoansVO> customersLoans = customersLoansMapper.selectStatusByCustomersId(customersId, status);
        if (customersLoans.size() != 0) {
            return new ResultVO(ResStatus.OK, "查询成功！", customersLoans);
        } else {
            return new ResultVO(ResStatus.NO, "查询失败！", null);
        }
    }
}
